const Router = require('koa-router');
const common = require('../libs/common');
const config = require('../config/config');
const response = require('../libs/result');
const db = require('../libs/db');
let router = new Router();
const fs = require('fs');

/**
 * 
 * @api {post} /api/v1/test/test  测试接口
 * @apiDescription 测试接口
 * @apiName test
 * @apiSampleRequest /api/v1/test/test
 * @apiGroup test
 * @apiVersion 1.0.0
 */
router.post('/test', async ctx => {
   let imgData= ctx.request.body.imgdata;
   var obj = {name: 'zhangsan', age: 8, ace: 5, nbme: 'lisi'};//要排序的对象
function objKeySort(obj) {//排序的函数
var newkey = Object.keys(obj).sort();
　　//先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
var newObj = {};//创建一个新的对象，用于存放排好序的键值对
for (var i = 0; i < newkey.length; i++) {//遍历newkey数组
newObj[newkey[i]] = obj[newkey[i]];//向新创建的对象中按照排好的顺序依次增加键值对
}
return newObj;//返回排好序的新对象
}

objKeySort(obj) ; //函数执行
console.log(objKeySort(obj))

    response(ctx, 200, objKeySort(obj), '测试接口');
})



/**
 * 
 * @api {post} /api/v1/test/test2  测试接口2
 * @apiDescription 测试接口2
 * @apiName test2
 * @apiParam (参数) {String} phone_number   手机号(81398506844)
 * @apiSampleRequest /api/v1/test/test2
 * @apiGroup test
 * @apiVersion 1.0.0
 */
router.post('/test2', async ctx => {
  let {company_name,company_province,company_address,company_phone,best_path,env_path,company_city,company_district,company_area,profession,salary,full_name,first_name,middle_name,last_name,birthday,province,city,district,area,address,gender,marital_status,children_number,residence_duration,family_name_in_lan,email,motherName,jobType,workTime,bankPhone,familyCard} = ctx.request.body;
    // let md='' + Math.floor(Math.random() * 999999);
    // ctx.body = common.handleResulte(200, md, '');

    let customers_customparams = await ctx.db.query('SELECT name,`key`,type,options  FROM customers_customparams',[]);
    let result = [];
    let i=0;
    customers_customparams.map(function(item,index,input){
      result[i]=item.key;
      i++
    });
    let qstr=result.join(',lu.');
})

/**
 * 
 * @api {get} /api/v1/test/test3  测试接口3-签名算法
 * @apiDescription 测试接口3
 * @apiName test3
 * @apiSampleRequest /api/v1/test/test3
 * @apiGroup test
 * @apiVersion 1.0.0
 */
router.get('/test3', async ctx => {
    // let obj = {
    //     "id": 1,
    //     "status": 'ACTIVE'
    // };
    // let data = common.signjsapi(obj);
    // response(ctx, 200, data, '测试签名算法');
    // ctx.body = common.handleResulte(200, data, '测试签名算法');

    let str='[[591,6],[614,7],[614,7],[618,7],[618,7],[638,7],[638,7],[638,7],[648,11],[648,11],[648,11],[648,11],[649,11],[649,11],[649,11],[649,11],[652,8],[653,11],[653,11],[653,11],[656,8],[660,12],[664,10],[665,12],[670,6],[671,7],[673,6],[675,11],[676,8],[677,7]]'

   let res= eval("(" + str + ")");
})

/**
 * @api {get} /api/v1/test/test4  测试接口4-第三方接口调用
 * @apiDescription 测试接口4
 * @apiName test4
 * @apiSampleRequest /api/v1/test/test4
 * @apiGroup test
 * @apiVersion 1.0.0
 */
router.get('/test4', async ctx => {
    let data = await common.webclient(config.outfallUrl1 + '/api/web_version', {}, 'get');
    response(ctx, 200, data, '测试第三方接口调用');
})

/**
 * @api {get} /api/v1/test/test5  测试接口5-新增用户
 * @apiDescription 测试接口5
 * @apiName test5
 * @apiSampleRequest /api/v1/test/test5
 * @apiGroup test
 * @apiVersion 1.0.0
 */
router.get('/test5', async ctx => {
    let data = ctx.db.query(`INSERT INTO loan_user (created_time,updated_time,is_active,mobile, name, id_card_number, user_type,education,religion,income,loan_remark) 
    VALUES(?,?,?,?,?,?,?,?,?,?,?)`, [new Date(), new Date(), 1, 130,1,1,1,1,1,1,1]);
    response(ctx, 200, data, '测试新增用户调用');
})
/**
 * @api {get} /api/v1/test/test6  测试接口6
 * @apiDescription 测试接口6
 * @apiName test6
 * @apiSampleRequest /api/v1/test/test6
 * @apiGroup test
 * @apiVersion 1.0.0
 */
router.get('/test6', async ctx => {
    let data = await db.exists(ctx, "auth_user", {phone_number:'81398506844 or 1=1 ', username:'81398506844'});
    response(ctx, 200, data, '测试新增用户调用');
})

module.exports = router.routes()